package com.shop.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.domain.Brand;
import com.shop.goods.mapper.BrandMapper;
import com.shop.goods.service.BrandService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
* @author 靳润波
* @description 针对表【goods_brand】的数据库操作Service实现
* @createDate 2024-07-27 09:36:37
*/
@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public List<Brand> selectAll() {
        LambdaQueryWrapper <Brand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Brand::getIsDelete,0);
        return brandMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Brand brand) {
        return brandMapper.insert(brand);
    }

    @Override
    public Integer delete(Long id) {
        Brand brand = new Brand();
        brand.setId(id);
        brand.setIsDelete(1);
        brandMapper.updateById(brand);
        return 1;
    }

    @Override
    public Integer update(Brand brand) {
        return brandMapper.updateById(brand);
    }

    @Override
    public Integer status(Long id) {
        Brand brand = brandMapper.selectById(id);
        if (brand == null){
            return -1;
        }
        if (brand.getStatus() == 1) {
            brand.setStatus(0);
        }else {
            brand.setStatus(1);
        }
        return brandMapper.updateById(brand);

    }

    @Override
    public Integer status(Long id, Integer status) {
        Brand brand = new Brand();
        brand.setId(id);
        brand.setStatus(status);
        return brandMapper.updateById(brand);

    }

    @Override
    public List<Brand> selectList(Brand brand) {
        LambdaQueryWrapper lqw = this.lqw(brand);
        return brandMapper.selectList(lqw);
    }

    @Override
    public Page<Brand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<Brand>) this.selectAll();
    }

    @Override
    public Page<Brand> pageList(Brand brand, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<Brand>) this.selectList(brand);
    }

    public LambdaQueryWrapper lqw(Brand iBrand){
        LambdaQueryWrapper <Brand> lqw = new LambdaQueryWrapper<>();
        if (iBrand.getId() != null){
            lqw.eq(Brand::getId,iBrand.getId());
        }
        if (iBrand.getBrandName() != null){
            lqw.eq(Brand::getBrandName,iBrand.getBrandName());
        }
        if (iBrand.getBrandLogo() != null){
            lqw.eq(Brand::getBrandLogo,iBrand.getBrandLogo());
        }
        if (iBrand.getStatus() != null){
            lqw.eq(Brand::getStatus,iBrand.getStatus());
        }
        if (iBrand.getIsDelete() != null){
            lqw.eq(Brand::getIsDelete,iBrand.getIsDelete());
        }
        if (iBrand.getCreateTime() != null){
            lqw.eq(Brand::getCreateTime,iBrand.getCreateTime());
        }
        if (iBrand.getUpdateTime() != null){
            lqw.eq(Brand::getUpdateTime,iBrand.getUpdateTime());
        }
        return lqw;
    }

}




